package models

import (
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type MainPageViewModel struct {
	TotalCustomer    int     `json:"totalCustomer" orm:"column(total_customer)"`
	NewTodayCustomer int     `json:"newTodayCustomer" orm:"column(today_customer)"`
	NewMonthCustomer int     `json:"newMonthCustomer" orm:"column(month_customer)"`
	TotalMoney       float32 `json:"totalMoney" orm:"column(total_budget)"`
	NewMonthMoney    float32 `json:"newMonthMoney" orm:"column(month_budget)"`
}

// GetView 查询主页信息
func GetView() (MainPageViewModel, error) {
	var view MainPageViewModel
	o := orm.NewOrm()
	err := o.Raw("select count(*) total_customer from customers").QueryRow(&view.TotalCustomer)
	err = o.Raw("select count(*) today_customer from customers where customer_come_date = DATE(NOW()) ").QueryRow(&view.NewTodayCustomer)
	err = o.Raw("select count(*) month_customer from customers where customer_come_date between DATE_SUB(now(),interval 1 month) and now()").QueryRow(&view.NewMonthCustomer)
	err = o.Raw("select sum(service_budget) total_budget from services").QueryRow(&view.TotalMoney)
	err = o.Raw("select sum(service_budget) month_budget from services where start_time between DATE_SUB(now(),interval 1 month) and now()").QueryRow(&view.NewMonthMoney)
	fmt.Println(view)
	if err != nil {
		return MainPageViewModel{}, err
	} else {
		return view, nil
	}
}

type MainPageColumnViewModel struct {
	NewCustomers  [5]int    `json:"newCustomers"`
	LostCustomers [5]int    `json:"lostCustomers"`
	Dates         [5]string `json:"dates"`
}

func (m *MainPageColumnViewModel) init() {
	t := time.Now()
	m.Dates[4] = t.Format("2006-1-2")
	m.Dates[3] = t.AddDate(0, 0, -7).Format("2006-1-2")
	m.Dates[2] = t.AddDate(0, 0, -14).Format("2006-1-2")
	m.Dates[1] = t.AddDate(0, 0, -21).Format("2006-1-2")
	m.Dates[0] = t.AddDate(0, 0, -28).Format("2006-1-2")
}

// GetCustomerCount 查询主页图表信息
func GetCustomerCount() (MainPageColumnViewModel, error) {
	mpcv := MainPageColumnViewModel{}
	mpcv.init()
	o := orm.NewOrm()
	for i := 0; i < len(mpcv.NewCustomers)-1; i++ {
		err := o.Raw("select count(*) from customers where customer_come_date between ? and ?", mpcv.Dates[i], mpcv.Dates[i+1]).QueryRow(&mpcv.NewCustomers[i])
		err = o.Raw("select count(*) from customers where customer_status = 2 and customer_come_date between ? and ?", mpcv.Dates[i], mpcv.Dates[i+1]).QueryRow(&mpcv.LostCustomers[i])
		if err != nil {
			return MainPageColumnViewModel{}, err
		}
	}
	return mpcv, nil
}

type CycleServiceInfo struct {
	CycleView [5]int `json:"cycleView"`
}

// GetServiceInfo 查询主页饼图信息
func GetServiceInfo() (CycleServiceInfo, error) {
	c := CycleServiceInfo{}
	o := orm.NewOrm()
	err := o.Raw("select count(*) from services where service_status = -1").QueryRow(&c.CycleView[0])
	err = o.Raw("select count(*) from services where service_status = 0").QueryRow(&c.CycleView[1])
	err = o.Raw("select count(*) from services where service_status = 1").QueryRow(&c.CycleView[2])
	err = o.Raw("select count(*) from services where service_status = 2").QueryRow(&c.CycleView[3])
	err = o.Raw("select count(*) from services where service_status = 3").QueryRow(&c.CycleView[4])
	if err != nil {
		return CycleServiceInfo{}, err
	} else {
		return c, nil
	}

}
